#!/usr/bin/env python
# coding: utf-8
# Copyright © Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.

import subprocess
import os

# 定制化脚本，由manifest构建时调用
class Customization(object):
    def __init__(self, board_name, rootfs_path):
        self.board_name = board_name
        self.rootfs_path = rootfs_path
        self.profile_name = "include/profile.txt"

    def post_image(self):
        profile = os.path.join(self.rootfs_path, self.profile_name)
        with open(profile) as fp:
            for line in fp:
                line = line.replace("\n", "")
                line = line.replace(" ", "")
                if 0 == len(line):
                    continue
                index = len(line) - 1
                while(line[index] != "/"):
                    index = index - 1
                
                file_path = os.path.join(self.rootfs_path, "include", line)
                if os.path.exists(file_path):
                    file_name = line[index + 1:len(line)]
                    _, ext = os.path.splitext(file_name)
                    subprocess.run(f"cp {file_path} {os.path.join(self.rootfs_path, 'opt', 'bmc', ext[1:], file_name)}", shell=True)

